package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.CartForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;

import org.apache.dubbo.config.annotation.Reference;
import org.apache.zookeeper.proto.DeleteRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class CartController {
    @Reference
    ICartService cartService;

    @GetMapping("/shopping/carts")
    public ResponseData cartList(HttpServletRequest request){
        String tokenStr = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject jsonObject = JSON.parseObject(tokenStr);
        Long uid = jsonObject.getLong("uid");
//        Long uid = new Long(1);
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);

        CartListByIdResponse cartList = cartService.getCartListById(cartListByIdRequest);

        if (!ShoppingRetCode.SUCCESS.getCode().equals(cartList.getCode())) {
            return new ResponseUtil<>().setErrorMsg(cartList.getMsg());
        }
        return new ResponseUtil<>().setData(cartList.getCartProductDtos());
    }

    @PostMapping("/shopping/carts")
    public ResponseData addTOCart(@RequestBody CartForm cartForm){
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(cartForm.getUserId());
        addCartRequest.setItemId(cartForm.getProductId());
        addCartRequest.setNum(cartForm.getProductNum());
        AddCartResponse addCartResponse = cartService.addToCart(addCartRequest);
        if(!ShoppingRetCode.SUCCESS.getCode().equals(addCartResponse.getCode())){
            return new ResponseUtil<>().setErrorMsg(addCartResponse.getMsg());
        }
        return new ResponseUtil<>().setData(null);
    }

    @PutMapping("/shopping/carts")
    public ResponseData updateCart(@RequestBody CartForm cartForm) {
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setUserId(cartForm.getUserId());
        request.setChecked(cartForm.getChecked());
        request.setItemId(cartForm.getProductId());
        request.setNum(cartForm.getProductNum());
        UpdateCartNumResponse response = cartService.updateCartNum(request);
        if(response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteCart(@PathVariable Long uid , @PathVariable Long pid){
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);
        DeleteCartItemResponse response = cartService.deleteCartItem(deleteCartItemRequest);

        if(response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response.getMsg());
        }
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/shopping/items/{id}")
    public ResponseData deleteCart(@PathVariable Long id){
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(id);
        DeleteCheckedItemResposne resposne = cartService.deleteCheckedItem(deleteCheckedItemRequest);
        if(resposne.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(resposne.getMsg());
        }

        return new ResponseUtil<>().setErrorMsg(resposne.getMsg());
    }
}
